package com.gwtgeo.ui.client.mvp.newmap;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.gwtgeo.service.model.WMSLayerDef;
import com.gwtgeo.ui.client.mvp.adapter.HasTextFormItemAdapter;
import com.gwtgeo.ui.client.mvp.adapter.HasValueFormItemAdapter;
import com.gwtgeo.ui.client.mvp.windowsmanager.WindowedDisplay;
import com.gwtgeo.ui.client.mvp.windowsmanager.WindowedView;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.HasClickHandlers;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.SpinnerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.HasChangedHandlers;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class NewMapView extends WindowedView<VLayout> implements
		NewMapPresenter.Display {

	private DynamicForm form;
	private TextItem nameItem;
	private SpinnerItem longitudeItem;
	private SpinnerItem latitudeItem;
	private SelectItem projectionItem;
	private SelectItem baseLayerItem;
	private IButton acceptButton;

	private IButton cancelButton;

	public NewMapView() {
		super();
	}

	@Override
	public HasClickHandlers getAcceptClick() {
		return this.acceptButton;
	}

	@Override
	public HasText getName() {
		return new HasTextFormItemAdapter(this.nameItem);
	}

	@Override
	public HasText getProjection() {
		return new HasTextFormItemAdapter(this.projectionItem);
	}

	public HasValue<Float> getLongitude() {
		this.latitudeItem.getValue();
		return new HasValueFormItemAdapter<Float>(this.longitudeItem);
	}

	public HasValue<Float> getLatitude() {
		return new HasValueFormItemAdapter<Float>(this.latitudeItem);
	}

	@Override
	protected VLayout buildWidget() {

		VLayout widget = new VLayout();

		form = new DynamicForm();

		this.nameItem = new TextItem("fileName", "File name");
		this.nameItem.setWidth(250);
		this.nameItem.setRequired(true);

		this.projectionItem = new SelectItem("projection", "Projection");
		LinkedHashMap<String, String> projectionValueMap = new LinkedHashMap<String, String>();
		projectionValueMap.put("EPSG:4326", "EPSG:4326");
		this.projectionItem.setWidth(250);
		this.projectionItem.setRequired(true);
		this.projectionItem.setValueMap(projectionValueMap);

		this.baseLayerItem = new SelectItem("baselayer", "Base Layer");
		this.baseLayerItem.setWidth(250);
		this.baseLayerItem.setRequired(true);

		this.longitudeItem = new SpinnerItem("longitude", "Longitude");
		this.longitudeItem.setDefaultValue(-3.699875);
		this.longitudeItem.setMin(-180);
		this.longitudeItem.setMax(180);
		this.longitudeItem.setStep(0.000001);
		this.longitudeItem.setRequired(true);

		this.latitudeItem = new SpinnerItem("latitude", "Latitude");
		this.latitudeItem.setDefaultValue(40.434718);
		this.latitudeItem.setMin(-90);
		this.latitudeItem.setMax(90);
		this.latitudeItem.setStep(0.000001);
		this.latitudeItem.setRequired(true);

		form.setItems(this.nameItem, this.projectionItem, this.baseLayerItem,
				this.longitudeItem, this.latitudeItem);
		widget.addMember(form);

		HLayout buttonsPanel = new HLayout();

		acceptButton = new IButton("Accept");
		cancelButton = new IButton("Cancel");

		buttonsPanel.addMember(acceptButton);
		buttonsPanel.addMember(cancelButton);
		buttonsPanel.setHeight(25);
		buttonsPanel.setMembersMargin(5);

		widget.addMember(buttonsPanel);

		return widget;
	}

	@Override
	protected int getWindowHeight() {
		return 200;
	}

	@Override
	protected String getWindowTitle() {
		return "New Map";
	}

	@Override
	protected int getWindowWidth() {
		return 400;
	}

	@Override
	public HasClickHandlers getCancelClick() {
		return cancelButton;
	}

	@Override
	public boolean validate() {
		return this.form.validate();
	}

	@Override
	protected void addHeaderControls(Map<String, HasClickHandlers> headers) {
		window.setHeaderControls(HeaderControls.HEADER_LABEL, headers
				.get(WindowedDisplay.CLOSE_HEADER));
	}

	@Override
	public void setBaseLayers(List<WMSLayerDef> layers) {

		LinkedHashMap<String, String> baseLayers = new LinkedHashMap<String, String>();
		for (int i = 0; i < layers.size(); i++) {
			baseLayers.put(Integer.toString(i),layers.get(i).getName());
		}
		this.baseLayerItem.setValueMap(baseLayers);
	}

	@Override
	public HasChangedHandlers getProjectionChange() {
		return this.projectionItem;
	}

	@Override
	public HasText getBaseLayer() {
		return new HasTextFormItemAdapter(this.baseLayerItem);
	}

}
